// subpage/pages/login/login.js
const app = getApp();
import service from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginWorld:'授权手机号',//登录按钮的名称
    isShow: false,
    userinfo: app.globalData.userinfo,
    token: app.globalData.token,
    code: '',
    goback:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('登录页',options)

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.navigateBack()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getUserInfo: function (e) {
    let that = this
    console.log('登陆信息',e)
    // wx.setStorageSync('userinfo', e.detail.userInfo)
    let userInfo = JSON.parse(e.detail.rawData)
    console.log('用户信息',JSON.parse(e.detail.rawData))
    let sign = e.detail.signature
    that.setData({
      loginWorld: "登陆中..."
    })
    that.login(res,userInfo)
    },
    //新版获取用户信息
  getUserProfile: function () {
    let that = this
    let code = ''
    wx.login({
      success: (result => {
        console.log('codediyic',result)
        that.setData({
          code: result.code
        })
      })
    })
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log('sssssss',res)
        let userInfo = res.rawData
        console.log('用户信息',JSON.parse(res.rawData))
        let sign = res.signature
        that.setData({
          loginWorld: "登陆中..."
        })
        // that.login(res,userInfo)
        wx.request({
          url: app.data.url+'/login/code',
          method: "POST",
          data: {
            code:that.data.code,
            sign:res.signature,
            rawData: userInfo,
            encryptedData:res.encryptedData,
            iv: res.iv
          },
          success: function (result) {
            console.log('登陆成功',result)
            if(result.data.code == 0){
              wx.setStorageSync('token', result.data.token.token)
              wx.setStorageSync('userPhone', result.data.token.perfectPhone)
              wx.setStorageSync('userinfo', userInfo)
              // that.getusers()
              if(!result.data.token.perfectPhone){
                that.showPhoneBtn()
              }else{
                wx.navigateBack()
              }
            }else{
              wx.showToast({
                title: '获取信息失败',
                icon: 'none'
              })
            }
          }
        })
      }
    })
    // wx.setStorageSync('userinfo', e.detail.userInfo)
 },
 login (resful, userInfo) {
   let that = this
  wx.login({
    success: res => {
      console.log('我获取code',res)
      wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log('sssssss',res)
        let userInfo = JSON.parse(res.rawData)
        console.log('用户信息',JSON.parse(res.rawData))
        let sign = res.signature
        that.setData({
          loginWorld: "登陆中..."
        })
        // that.login(res,userInfo)
        
      }
    })
      return
      //获取到code，发送给后台获取openid
      //获取openid成功后，判断是否绑定手机号
      wx.request({
        url: app.data.url+'/login/code',
        method: "POST",
        data: {
          code:res.code,
          sign:resful.signature,
          rawData: userInfo,
          encryptedData:resful.encryptedData,
          iv: resful.iv
        },
        success: function (result) {
          console.log('登陆成功',result)
          if(result.data.code == 0){
            wx.setStorageSync('token', result.data.token.token)
            wx.setStorageSync('userPhone', result.data.token.perfectPhone)
            wx.setStorageSync('userinfo', JSON.stringify(userInfo))
            // that.getusers()
            if(!result.data.token.perfectPhone){
              that.showPhoneBtn()
            }else{
              wx.navigateBack()
            }
          }else{
            wx.showToast({
              title: '获取信息失败',
              icon: 'none'
            })
          }
        }
      })
    },
  })
 },
  //获取个人信息
  getusers(){
    let data = service('GET','/user/info',{}).then( res => {
      console.log('我获取了我的头像',res)
    })
  },
  showPhoneBtn () {
    this.setData({
      isShow: true
    })
  },
  getPhoneNumber (e) {
    console.log('我获取了手机号', e)
    wx.request({
      url: app.data.url+'/login/phone',
      method: "POST",
      header:{
        "token": wx.getStorageSync('token') || ''
      },
      data: {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
      },
      success: function (res) {
        console.log('手机号返回值',res)
        if(res.data.code == 0){
          wx.setStorageSync('userPhone',true)
          wx.navigateBack()
        }
      }
    })
  },
  //关闭弹窗
  closeShow () {
    console.log('我要关闭弹窗了')
    this.setData({
      isShow: false
    })
  }
})